package com.haina.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.haina.shop.constant.MessageConstant;
import com.haina.shop.domain.IndexImg;
import com.haina.shop.domain.Notice;
import com.haina.shop.result.IPageIndexImg;
import com.haina.shop.result.ServiceResult;
import com.haina.shop.service.NoticeService;
import com.haina.shop.mapper.NoticeMapper;
import com.haina.shop.vo.NoticeVO;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
* @author 48077
* @description 针对表【tz_notice】的数据库操作Service实现
* @createDate 2024-07-31 18:34:29
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{
    @Autowired
    private NoticeMapper mapper;
    /**
     * 获取所有置顶公告列表信息
     */
    @Override
    public ServiceResult<List<NoticeVO>> getTopNoticeList() {
        List<Notice> noticeList=mapper.selectByIsTop();
        List<NoticeVO> noticeVOList=new ArrayList<>();
        for (Notice notice : noticeList) {
            NoticeVO vo = getVO(notice);
            noticeVOList.add(vo);
        }
        return ServiceResult.success(noticeVOList);
    }
    /**
     * 获取所有公告列表信息
     * @return
     */
    @Override
    public ServiceResult<IPageIndexImg<NoticeVO>> getNoticeList() {
        //设置分页初始值
        PageHelper.startPage(Math.toIntExact(MessageConstant.PAGE_CURRENT),
                Math.toIntExact(MessageConstant.PAGE_SIZE));
        //调用mapper层进行分页查询
        Page<Notice> noticePage=mapper.page();
        //创建IPageIndexImg对象
        IPageIndexImg<NoticeVO> iPageIndexImg=new IPageIndexImg<>();
        iPageIndexImg.setSize(MessageConstant.PAGE_SIZE);
        iPageIndexImg.setTotal(noticePage.getTotal());
        iPageIndexImg.setCurrent(MessageConstant.PAGE_CURRENT);
        iPageIndexImg.setPages(noticePage.getTotal()/ MessageConstant.PAGE_SIZE+1);
        //获取所有记录
        List<Notice> result = noticePage.getResult();
        List<NoticeVO> data = new ArrayList<>();
        //属性拷贝
        for (Notice notice : result) {
            data.add(getVO(notice));
        }
        iPageIndexImg.setRecords(data);
        //返回结果集
        return ServiceResult.success(iPageIndexImg);
    }
    /**
     * 获取公告id公告详情
     * @param id
     * @return
     */
    @Override
    public ServiceResult<NoticeVO> getDataById(Long id) {
        Notice notice = getById(id);
        return ServiceResult.success(getVO(notice));
    }

    //entity-VO
    public NoticeVO getVO(Notice notice){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return new NoticeVO(
                notice.getId(), notice.getShopId(),notice.getTitle(),notice.getContent(),
                sdf.format(notice.getPublishTime())
        );
    }
}




